<?php

/* ----------------------------------------------
  Caloris: Moudle Settings

  $Id$
  $Author$

  Soubor nastavení
  ---------------------------------------------- */

require_once("../../../index.php");
Caloris\ACL::authenticate();

define('_MODULE_ACTIVE', 'administrator');

function checkb($input, $val) {
    if ($input == 1) {
        $content = '<input type="radio" name="' . $val . '" value="1" checked="checked" /> ano <input type="radio" name="' . $val . '" value="0" /> ne';
    } else {
        $content = '<input type="radio" name="' . $val . '" value="1" /> ano <input type="radio" name="' . $val . '" value="0" checked="checked" /> ne';
    }

    return $content;
}

if ($_REQUEST["function_name"] == 'settings_main') {
    if (Caloris\ACL::authorize("settings") == 1) {
        $file = _CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/settings.xml';
        $xml = simplexml_load_file($file);

        if ($_POST["wysiwyg"] != 1) {
            $_POST["wysiwyg"] = 0;
        }

        $xml->basic->title = $_POST["xtitle"];
        $xml->basic->title_short = $_POST["title_short"];
        $xml->basic->wysiwyg = $_POST["wysiwyg"];
        $xml->basic->template = $_POST["template"];
        $xml->basic->mod_rewrite = $_POST["mod_rewrite"];
        $xml->asXML($file);

        header("location: " . $_SERVER["HTTP_REFERER"] . "");
        exit();
    } else {
        header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/administrator/settings.php?msg=" . urlencode("Nemáte oprávnění"));
        exit();
    }
} elseif ($_REQUEST["function_name"] == 'settings_database') {
    if (Caloris\ACL::authorize("permissions") == 1) {
        $file = _CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/settings.xml';
        $xml = simplexml_load_file($file);

        $xml->database->type = $_POST["type"];
        $xml->database->address = $_POST["address"];
        $xml->database->user = $_POST["user"];
        $xml->database->name = $_POST["name"];
        $xml->database->password = $_POST["password"];
        $xml->asXML($file);

        header("location: " . $_SERVER["HTTP_REFERER"] . "");
        exit();
    } else {
        header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/administrator/settings.php?msg=" . urlencode("Nemáte oprávnění"));
        exit();
    }
} elseif ($_REQUEST["function_name"] == 'sitemap_generate') {
    // Výpis stránek
    $xma = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml');

    if (count($xma->page->children()) > 0) {
        $urls .= '<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="gss.xsl"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url><loc>' . _CALSET_PATHS_URI . '</loc></url>
';

        for ($e = 0; $e < count($xma->page); $e++) {
            if ($xma->page[$e]->sitemap == 0 || $xma->page[$e]["id"] == 'index') {
                
            } else {
                $urls .= '<url><loc>' . _CALSET_PATHS_URI . '/' . $xma->page[$e]["id"] . '</loc></url>
';
            }
        }

        // Výpis kategorii
        $xml = simplexml_load_file(_CALSET_PATHS_BASE . '/cal-components/data/categories.xml');
        $result = $xml->xpath('//category[@id="37"]');

        while (list(, $node) = each($result)) {
            $res .= $node->asXML();
        }

        preg_match_all('/<category id="([0-9]*)">/s', $res, $categoriesBlog);

        $xml = simplexml_load_string($res);

        for ($e = 0; $e < count($categoriesBlog[1]); $e++) {
            $result[$e] = $xml->xpath('//category[@id="' . $categoriesBlog[1][$e] . '"]');
            $urls .= '<url><loc>' . _CALSET_PATHS_URI . '/katalog-kategorie/' . $result[$e][0]->short . '</loc></url>' . PHP_EOL;
        }

        $urls .= '</urlset>';

        $file = fopen(_CALSET_PATHS_BASE . '/sitemap.xml', 'w', 1);
        fwrite($file, $urls);
        fclose($file);
    }

    header("location: " . $_SERVER["HTTP_REFERER"] . "");
    exit();
}

if (_CALSET_DATABASE_TYPE == 'mysql' && _CALSET_DATABASE_USER != '') {
    $xl = new Caloris\Database();
    $xl->query("SELECT CHARSET('abc') AS result;");
    $encdb = $xl->cell(0, "result");
}

if (_CALSET_DATABASE_TYPE == 'mysql' && _CALSET_DATABASE_USER != '') {
    $xl = new Caloris\Database();
    $xl->query("SELECT VERSION() AS result;");
    $verdb = $xl->cell(0, "result");
}

if (function_exists("apache_get_version")) {
    $apgetv = apache_get_version();
} else {
    $apgetv = 'Není známa';
}

$path = _CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/settings.xml';
$fd = fopen($path, "r");

if (filesize($path) > 0) {
    $typer = fread($fd, filesize($path));
}

$xml = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/settings.xml');

if ($xml->basic->wysiwyg == 1) {
    $wysiwyg = ' checked="checked"';
}

if ($xml->basic->mod_rewrite == 1) {
    $mod_rewrite = ' checked="checked"';
}

$content = '
<h1>Nastavení</h1>

<p style="padding-bottom: 10px;">Hlavní nastavení stránky. Nastavení v Page Editoru vždy přepíše nastavení pro stránky.</p>

<h2>Hlavní</h2>

<form action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/administrator/settings.php" method="post" class="form-horizontal">
<input type="hidden" name="function_name" value="settings_main" />

<div class="control-group">
<label class="control-label">Název stránky</label>
<div class="controls"><input type="text" value="' . $xml->basic->title . '" name="xtitle" /></div>
</div>

<div class="control-group">
<label class="control-label">Krátký název pro stránku</label>
<div class="controls"><input type="text" name="title_short" value="' . $xml->basic->title_short . '" /></div>
</div>

<div class="control-group">
<label class="control-label">Používaná šablona</label>
<div class="controls">';

$xma = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/templates.xml');

if (count($xma) > 0) {
    $content .= '<select name="template">
' . PHP_EOL;

    for ($a = 0; $a < count($xma); $a++) {
        if (strcmp($xml->basic->template, $xma[0]->template[$a]->name) == 0) {
            $content .= '<option value="' . $xma[0]->template[$a]->name . '" selected="selected">' . $xma[0]->template[$a]->name . '</option>';
        } else {
            $content .= '<option value="' . $xma[0]->template[$a]->name . '">' . $xma[0]->template[$a]->name . '</option>';
        }
    }

    $content .= '
</select>';
}

$content .= '</div>
</div>

<div class="control-group">
<label class="control-label">Povolit mod_rewrite</label>
<div class="controls"><input type="checkbox" name="mod_rewrite" value="1" ' . $mod_rewrite . ' /></div>
</div>

<div class="control-group">
<label class="control-label">Povolit zabudovaný editor</label>
<div class="controls"><input type="checkbox" name="wysiwyg" value="1" ' . $wysiwyg . ' /></div>
</div>

<div class="control-group">
<div class="controls"><input type="submit" value="Uložit" class="btn btn-primary" /></div>
</div>
</form>

<h2>Databáze</h2>' . PHP_EOL;

if (Caloris\ACL::authorize("permissions") == 1) {
    $content .= '
<form action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/administrator/settings.php" method="post" class="form-horizontal">
<input type="hidden" name="function_name" value="settings_database" />

<div class="control-group">
<label class="control-label">Typ databáze</label>
<div class="controls">
<input type="text" name="type" value="' . $xml->database->type . '" class="input-large" list="base" />
<datalist id="base">
   <option value="mysql">
   <option value="pgsql">
</datalist>
</div>
</div>

<div class="control-group">
<label class="control-label">Jméno databáze</label>
<div class="controls"><input type="text" name="name" value="' . $xml->database->name . '" class="input-large" /></div>
</div>

<div class="control-group">
<label class="control-label">Uživatelské jméno</label>
<div class="controls"><input type="text" name="user" value="' . $xml->database->user . '" class="input-large" /></div>
</div>

<div class="control-group">
<label class="control-label">Heslo k databázi</label>
<div class="controls"><input type="password" name="password" value="' . $xml->database->password . '" class="input-large" /></div>
</div>

<div class="control-group">
<div class="controls"><input type="submit" value="Uložit" class="btn btn-primary" /></div>
</div>
</form>' . PHP_EOL;
} else {
    $content .= '<p>Nejste oprávnění měnit nastavení databáze. Požádejte administrátora</p>' . PHP_EOL;
}

$content .= '
<h2>Nastavení systému</h2>

<table class="table table-striped">
    <tr>
        <td style="width: 400px;">Operační systém</td>
        <td>' . PHP_OS . '</td>
    </tr>
    <tr>
        <td>Verze PHP</td>
        <td>' . PHP_VERSION . '</td>
    </tr>
    <tr>
        <td>Verze Apache</td>
        <td>' . $apgetv . '</td>
    </tr>
    <tr>
        <td>Kódování databáze</td>
        <td>' . $encdb . '</td>
    </tr>
    <tr>
        <td>Verze databáze</td>
        <td>' . $verdb . '</td>
    </tr>
</table>' . PHP_EOL;

$data["title"] = 'Nastavení';

$tmpl = new Caloris\Template;
$tmpl->input($content);
$tmpl->renderHead($data);
echo $tmpl->renderBody('caloris-admin');